import { loadEnv, defineConfig, ConfigEnv } from "vite";
import vueJsx from "@vitejs/plugin-vue-jsx";
import path from "node:path";
import vue from "@vitejs/plugin-vue";
import Pages from "vite-plugin-pages";
import VueSetupExtend from "vite-plugin-vue-setup-extend";
import AutoImport from "unplugin-auto-import/vite";
// vite.config.js
import Components from "unplugin-vue-components/vite";

// import dotenv from "dotenv";
// dotenv.config();

import {
  AntDesignVueResolver,
  ElementPlusResolver,
  VantResolver,
} from "unplugin-vue-components/resolvers";
// https://vitejs.dev/config/
export default (ppp: ConfigEnv) => {
  const { mode } = ppp;
  const root = process.cwd();
  const env = loadEnv(mode, root);
  return defineConfig({
    plugins: [
      vue(),
      // have to
      vueJsx(),
      Pages({
        dirs: "src/pages",
        exclude: ["**/components/*.vue"], // 排除在外的目录，即不将所有 components 目录下的 .vue 文件生成路由
      }),
      AutoImport({
        // targets to transform
        // include: [
        //   /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
        //   /\.vue$/,
        //   /\.vue\?vue/, // .vue
        //   /\.md$/, // .md
        // ],
        resolvers: [
          AntDesignVueResolver({
            resolveIcons: true,
          }),
        ],
        imports: [
          // presets
          "vue",
          "vue-router",
        ],
      }),
      Components({
        resolvers: [AntDesignVueResolver()],
        dts: true,
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
      }),
    ],
    resolve: {
      //设置路径别名
      alias: {
        "@/": `${path.resolve(__dirname, "src")}/`,
        "/#/": `${path.resolve(__dirname, "types")}/`,
      },
    },
  });
};
